﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;
using System.Drawing;

namespace CasaEngineCommon.Extension
{
	/// <summary>
	/// 
	/// </summary>
	static public class XMLExtension
	{
		#region Fields
        
        #endregion

        #region Properties

        #endregion

        #region Constructors

        #endregion

        #region Methods

		#region Read

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlEl_"></param>
		/// <param name="c_"></param>
		static public void Read(this XmlElement xmlEl_, ref Color c_)
		{
            Color.FromArgb(
                byte.Parse(xmlEl_.Attributes["a"].Value, CultureInfo.InvariantCulture.NumberFormat),
                byte.Parse(xmlEl_.Attributes["r"].Value, CultureInfo.InvariantCulture.NumberFormat),
                byte.Parse(xmlEl_.Attributes["g"].Value, CultureInfo.InvariantCulture.NumberFormat),
                byte.Parse(xmlEl_.Attributes["b"].Value, CultureInfo.InvariantCulture.NumberFormat)); 
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlEl_"></param>
		/// <param name="v_"></param>
		static public void Read(this XmlElement xmlEl_, ref Rectangle r_)
		{
            r_.X = int.Parse(xmlEl_.Attributes["x"].Value, CultureInfo.InvariantCulture.NumberFormat);
            r_.Y = int.Parse(xmlEl_.Attributes["y"].Value, CultureInfo.InvariantCulture.NumberFormat);
            r_.Width = int.Parse(xmlEl_.Attributes["width"].Value, CultureInfo.InvariantCulture.NumberFormat);
            r_.Height = int.Parse(xmlEl_.Attributes["height"].Value, CultureInfo.InvariantCulture.NumberFormat);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlEl_"></param>
		/// <param name="v_"></param>
		static public void Read(this XmlElement xmlEl_, ref Point p_)
		{
            p_.X = int.Parse(xmlEl_.Attributes["x"].Value, CultureInfo.InvariantCulture.NumberFormat);
            p_.Y = int.Parse(xmlEl_.Attributes["y"].Value, CultureInfo.InvariantCulture.NumberFormat);
		}

		#endregion

		#region Write

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlDoc_"></param>
		/// <param name="nodeName_"></param>
		static public XmlElement AddRootNode(this XmlDocument xmlDoc_, string nodeName_)
		{
			//let's add the XML declaration section
			XmlNode xmlnode = xmlDoc_.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			xmlDoc_.AppendChild(xmlnode);

			//let's add the root element
			XmlElement xmlElem = xmlDoc_.CreateElement("", nodeName_, "");
			xmlDoc_.AppendChild(xmlElem);

			return xmlElem;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlDoc_"></param>
		/// <param name="xmlElement_"></param>
		/// <param name="attributeName_"></param>
		/// <param name="value_"></param>
		static public void AddAttribute(this XmlDocument xmlDoc_, XmlElement xmlElement_, string attributeName_, string value_)
		{
			XmlAttribute att = xmlDoc_.CreateAttribute(attributeName_);
			att.Value = value_;
			xmlElement_.Attributes.Append(att);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlDoc_"></param>
		/// <param name="nodeName_"></param>
		/// <param name="val_"></param>
		/// <returns></returns>
		static public XmlElement CreateElementWithText(this XmlDocument xmlDoc_, string nodeName_, string val_)
		{
			XmlElement el = xmlDoc_.CreateElement(nodeName_);
			XmlText txtXML = xmlDoc_.CreateTextNode(val_);
			el.AppendChild(txtXML);

			return el;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlDoc_"></param>
		/// <param name="nodeName_"></param>
		/// <param name="c_"></param>
		static public XmlElement CreateElement(this XmlDocument xmlDoc_, string nodeName_, Color c_)
		{
			XmlAttribute attR = xmlDoc_.CreateAttribute("r");
			attR.Value = c_.R.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute attG = xmlDoc_.CreateAttribute("g");
            attG.Value = c_.G.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute attB = xmlDoc_.CreateAttribute("b");
            attB.Value = c_.B.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute attA = xmlDoc_.CreateAttribute("a");
            attA.Value = c_.A.ToString(CultureInfo.InvariantCulture.NumberFormat);

			XmlElement el = xmlDoc_.CreateElement(nodeName_);
			el.Attributes.Append(attR);
			el.Attributes.Append(attG);
			el.Attributes.Append(attB);
			el.Attributes.Append(attA);

			return el;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlDoc_"></param>
		/// <param name="nodeName_"></param>
		/// <param name="v_"></param>
		static public XmlElement CreateElement(this XmlDocument xmlDoc_, string nodeName_, Rectangle r_)
		{
			XmlAttribute att1 = xmlDoc_.CreateAttribute("x");
			att1.Value = r_.X.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute att2 = xmlDoc_.CreateAttribute("y");
			att2.Value = r_.Y.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute att3 = xmlDoc_.CreateAttribute("width");
			att3.Value = r_.Width.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute att4 = xmlDoc_.CreateAttribute("height");
			att4.Value = r_.Height.ToString(CultureInfo.InvariantCulture.NumberFormat);

			XmlElement el = xmlDoc_.CreateElement(nodeName_);
			el.Attributes.Append(att1);
			el.Attributes.Append(att2);
			el.Attributes.Append(att3);
			el.Attributes.Append(att4);

			return el;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="xmlDoc_"></param>
		/// <param name="nodeName_"></param>
		/// <param name="v_"></param>
		static public XmlElement CreateElement(this XmlDocument xmlDoc_, string nodeName_, Point p_)
		{
			XmlAttribute att1 = xmlDoc_.CreateAttribute("x");
			att1.Value = p_.X.ToString(CultureInfo.InvariantCulture.NumberFormat);
			XmlAttribute att2 = xmlDoc_.CreateAttribute("y");
			att2.Value = p_.Y.ToString(CultureInfo.InvariantCulture.NumberFormat);

			XmlElement el = xmlDoc_.CreateElement(nodeName_);
			el.Attributes.Append(att1);
			el.Attributes.Append(att2);

			return el;
		}

		#endregion

		#endregion
	}
}
